<?php
/**
 * User: hufeng
 * Date: 2017/10/11
 * Desc: 活动管理
 */

namespace Admin\Controller;
use Model\BannerModel;

class BannerController extends BaseController{
    public $bModel;
    public function __construct(){
        parent::__construct();
        $this->bModel = new BannerModel();
    }
    //banner列表
    public function index(){
        if($_POST['__hash__'] || !$_GET['p']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');
        $sessionArr['status']  = @$_POST['status'] ? $_POST['status'] : @$sessionParam['status'];
        $sessionArr['name']  = @$_POST['name'] ? $_POST['name'] : @$sessionParam['name'];
        session('searchParam',$sessionArr);
        $page = @$_GET['p'] ? $_GET['p'] : 1;

        $where = [];
        $where['status'] = array('neq',3);
        if($sessionArr['status']){
            $where['status'] = $sessionArr['status'];
        }
        if($sessionArr['name']){
            $where['title'] = array('like','%'.$sessionArr['name'].'%');
        }
        $resList = $this->bModel->search($where,$page,$this->pernum);
        $totalNum = $this->bModel->getTotalNum($where);
        $pageShow = getPage($totalNum,$this->pernum);
        //所有分类
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('totalNum',$totalNum);
        $this->assign('postArr',$sessionArr);
        $this->display('banner/index');
    }
    //新增banner
    public function addBanner(){
        if(IS_POST){
            $data = $_POST;
            $data['addtime'] = $this->dateNow;
            $id = $this->bModel->addData($data);
            $this->logModel->log('添加banner','banner',$id);
            $this->printOut(['code'=>0]);
        }
        //获取分组
        $this->display('banner/addbanner');
    }
    //编辑banner
    public function editBanner(){
        if(IS_POST){
            $where['id'] = $_POST['id'];
            unset($_POST['id']);
            $data = $_POST;
            $this->bModel->editData($where,$data);
            $this->logModel->log('编辑banner','banner',$where['id']);
            $this->printOut(['code'=>0]);
        }

        //获取组内容
        $this->assign('page',$_GET['page']);
        $selfInfo = $this->bModel->findData(['id'=>$_GET['id']]);
        $this->assign('selfInfo',$selfInfo);
        $this->display('banner/editbanner');
    }
    //冻结解冻banner
    public function freezeBanner(){
        $data['status'] = $_GET['status'] == 1 ? 2: 1;
        $where['id'] = $_GET['id'];
        $this->bModel->editData($where,$data);
        $this->logModel->log('冻结|解冻banner','banner',$_GET['id']);
        redirect($this->domianUrl.'/banner/index?p='.$_GET['page'].'&tip=1');
    }
    //删除banner
    public function delBanner(){
        $data['status'] = 3;
        $where['id'] = $_GET['id'];
        $this->bModel->editData($where,$data);
        $this->logModel->log('删除banner','banner',$_GET['id']);
        redirect($this->domianUrl.'/banner/index?p='.$_GET['page'].'&tip=1');
    }
    //修改排序
    public function editSort(){
        $data['sort'] = $_POST['sort'];;
        $where['id'] = $_POST['id'];
        $this->bModel->editData($where,$data);
        $this->logModel->log('修改排序','banner',$_POST['id']);
        $this->printOut(['code'=>0]);
    }
}